package com.hexacta.hrs.sandbox.tasks2.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hexacta.hrs.sandbox.tasks2.model.User;
import com.hexacta.hrs.sandbox.tasks2.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@ContextConfiguration(locations = "/spring-orm-config.xml")
public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2489941976537757304L;

	@Autowired
	private UserService userService;
	private User user;
	private String name;

	/*
	 * crea un usuario si no existia
	 */
	public String findUser() {

		User aUser = this.getUserService().findByName(this.getName());

		if (aUser == null) {
			aUser = this.getUserService().createUser(this.getName());
			this.getUserService().addUser(aUser);
		}

		this.setUser(aUser);
		return SUCCESS;

	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
